package C058;

/**
 * ClassName: c02
 * Package: C058
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/3 09:28
 * @Version 1.0
 * @Since 1.0
 */
public class c02 {
    public static void main(String[] args) {
        char[][] arr = {{'X','O','X','O','X','O'},{'O','X','O','X','O','X'},{'X','O','X','O','X','O'},{'O','X','O','X','O','X'}};

        solve(arr);


    }

    public static void solve(char[][] board) {
        int row = board.length;
        int col = board[0].length;

        // 第一行 最后一行 dfs
        for (int i = 0; i < col; i++) {
            if(board[0][i] == 'O') {
                dfs(board, 0, i);
            }

            if(board[row - 1][i] == 'O') {
                dfs(board, row - 1, i);
            }
        }

        // 第一列 和 最后一列
        for (int i = 1; i < row - 1; i++) {
            if(board[i][0] == 'O') {
                dfs(board, i, 0);
            }
            if(board[i][col - 1] == 'O') {
                dfs(board, i, col - 1);
            }
        }


        // 统计答案
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if(board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
                if(board[i][j] == 'F') {
                    board[i][j] = 'O';
                }
            }
        }

    }

    private static void dfs(char[][] board, int i, int j) {
        if(i == -1 || i == board.length || j == -1 || j == board[0].length || board[i][j] != 'O') {
            return;
        }

        board[i][j] = 'F';
        dfs(board, i - 1, j);
        dfs(board, i + 1, j);
        dfs(board, i, j - 1);
        dfs(board, i, j + 1);
    }
}
